
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.3 保护共享数据的方式 · C++并发编程实战（第2版）</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.4-chinese.html" />
    
    
    <link rel="prev" href="3.2-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Praise_for_the_first_edition.html">
            
                <a href="../Praise_for_the_first_edition.html">
            
                    
                    第一版的赞许
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../preface-chinese.html">
            
                <a href="../preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../acknowledgments-chinese.html">
            
                <a href="../acknowledgments-chinese.html">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../about_this_book-chinese.html">
            
                <a href="../about_this_book-chinese.html">
            
                    
                    关于本书
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../about_the_author-chinese.html">
            
                <a href="../about_the_author-chinese.html">
            
                    
                    关于作者
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../about_cover_illustration-chinese.html">
            
                <a href="../about_cover_illustration-chinese.html">
            
                    
                    关于封面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 你好，并发世界
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 何谓并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 为什么使用并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 并发和多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 开始入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter2/2.0-chinese.html">
            
                <a href="../chapter2/2.0-chinese.html">
            
                    
                    第2章 线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter2/2.1-chinese.html">
            
                <a href="../chapter2/2.1-chinese.html">
            
                    
                    2.1 线程的基本操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter2/2.2-chinese.html">
            
                <a href="../chapter2/2.2-chinese.html">
            
                    
                    2.2 传递参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter2/2.3-chinese.html">
            
                <a href="../chapter2/2.3-chinese.html">
            
                    
                    2.3 转移所有权
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter2/2.4-chinese.html">
            
                <a href="../chapter2/2.4-chinese.html">
            
                    
                    2.4 确定线程数量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter2/2.5-chinese.html">
            
                <a href="../chapter2/2.5-chinese.html">
            
                    
                    2.5 线程标识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../chapter2/2.6-chinese.html">
            
                <a href="../chapter2/2.6-chinese.html">
            
                    
                    2.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="3.0-chinese.html">
            
                <a href="3.0-chinese.html">
            
                    
                    第3章 共享数据
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="3.1-chinese.html">
            
                <a href="3.1-chinese.html">
            
                    
                    3.1 共享数据的问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="3.2-chinese.html">
            
                <a href="3.2-chinese.html">
            
                    
                    3.2 使用互斥量
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10.3" data-path="3.3-chinese.html">
            
                <a href="3.3-chinese.html">
            
                    
                    3.3 保护共享数据的方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="3.4-chinese.html">
            
                <a href="3.4-chinese.html">
            
                    
                    3.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter4/4.0-chinese.html">
            
                <a href="../chapter4/4.0-chinese.html">
            
                    
                    第4章 同步操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter4/4.1-chinese.html">
            
                <a href="../chapter4/4.1-chinese.html">
            
                    
                    4.1 等待事件或条件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter4/4.2-chinese.html">
            
                <a href="../chapter4/4.2-chinese.html">
            
                    
                    4.2 使用future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter4/4.3-chinese.html">
            
                <a href="../chapter4/4.3-chinese.html">
            
                    
                    4.3 限时等待
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter4/4.4-chinese.html">
            
                <a href="../chapter4/4.4-chinese.html">
            
                    
                    4.4 简化代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter4/4.5-chinese.html">
            
                <a href="../chapter4/4.5-chinese.html">
            
                    
                    4.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 内存模型和原子操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 内存模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 原子操作和原子类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 同步操作和强制排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 设计基于锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 并发设计的意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 基于锁的并发数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 设计更加复杂的数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 设计无锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 定义和意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 无锁数据结构的例子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 设计无锁数据结构的指导建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 并发设计
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 线程间划分工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 并发代码的性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 为多线程性能设计数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 设计并发代码的注意事项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 在实践中设计并发代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.6" data-path="../chapter8/8.6-chinese.html">
            
                <a href="../chapter8/8.6-chinese.html">
            
                    
                    8.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../chapter9/9.0-chinese.html">
            
                <a href="../chapter9/9.0-chinese.html">
            
                    
                    第9章 高级线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../chapter9/9.1-chinese.html">
            
                <a href="../chapter9/9.1-chinese.html">
            
                    
                    9.1 线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../chapter9/9.2-chinese.html">
            
                <a href="../chapter9/9.2-chinese.html">
            
                    
                    9.2 中断线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../chapter9/9.3-chinese.html">
            
                <a href="../chapter9/9.3-chinese.html">
            
                    
                    9.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter10/10.0-chinese.html">
            
                <a href="../chapter10/10.0-chinese.html">
            
                    
                    第10章 并行算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter10/10.1-chinese.html">
            
                <a href="../chapter10/10.1-chinese.html">
            
                    
                    10.1 并行化标准库算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter10/10.2-chinese.html">
            
                <a href="../chapter10/10.2-chinese.html">
            
                    
                    10.2 执行策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter10/10.3-chinese.html">
            
                <a href="../chapter10/10.3-chinese.html">
            
                    
                    10.3 C++标准库中的并行算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter10/10.4-chinese.html">
            
                <a href="../chapter10/10.4-chinese.html">
            
                    
                    10.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 测试和调试多线程应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 与并发相关的Bug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 定位并发Bug的技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../appendix_A/A.0-chinese.html">
            
                <a href="../appendix_A/A.0-chinese.html">
            
                    
                    附录A C++11特性简明参考(部分)
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../appendix_A/A.1-chinese.html">
            
                <a href="../appendix_A/A.1-chinese.html">
            
                    
                    A.1 右值引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.2" data-path="../appendix_A/A.2-chinese.html">
            
                <a href="../appendix_A/A.2-chinese.html">
            
                    
                    A.2 删除函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.3" data-path="../appendix_A/A.3-chinese.html">
            
                <a href="../appendix_A/A.3-chinese.html">
            
                    
                    A.3 默认函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.4" data-path="../appendix_A/A.4-chinese.html">
            
                <a href="../appendix_A/A.4-chinese.html">
            
                    
                    A.4 常量表达式函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.5" data-path="../appendix_A/A.5-chinese.html">
            
                <a href="../appendix_A/A.5-chinese.html">
            
                    
                    A.5 Lambda函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.6" data-path="../appendix_A/A.6-chinese.html">
            
                <a href="../appendix_A/A.6-chinese.html">
            
                    
                    A.6 变参模板
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.7" data-path="../appendix_A/A.7-chinese.html">
            
                <a href="../appendix_A/A.7-chinese.html">
            
                    
                    A.7 自动推导变量类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.8" data-path="../appendix_A/A.8-chinese.html">
            
                <a href="../appendix_A/A.8-chinese.html">
            
                    
                    A.8 线程本地变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.9" data-path="../appendix_A/A.9-chinese.html">
            
                <a href="../appendix_A/A.9-chinese.html">
            
                    
                    A.9 模板类参数的推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.10" data-path="../appendix_A/A.10-chinese.html">
            
                <a href="../appendix_A/A.10-chinese.html">
            
                    
                    A.10 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../appendix_B/B.0-chinese.html">
            
                <a href="../appendix_B/B.0-chinese.html">
            
                    
                    附录B 并发库简要对比
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../appendix_C/C.0-chinese.html">
            
                <a href="../appendix_C/C.0-chinese.html">
            
                    
                    附录C 消息传递框架与完整的ATM示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../appendix_D/D.0-chinese.html">
            
                <a href="../appendix_D/D.0-chinese.html">
            
                    
                    附录D C++线程类库参考
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.22.1" data-path="../appendix_D/D.1-chinese.html">
            
                <a href="../appendix_D/D.1-chinese.html">
            
                    
                    D.1 chrono头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.2" data-path="../appendix_D/D.2-chinese.html">
            
                <a href="../appendix_D/D.2-chinese.html">
            
                    
                    D.2 condition_variable头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.3" data-path="../appendix_D/D.3-chinese.html">
            
                <a href="../appendix_D/D.3-chinese.html">
            
                    
                    D.3 atomic头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.4" data-path="../appendix_D/D.4-chinese.html">
            
                <a href="../appendix_D/D.4-chinese.html">
            
                    
                    D.4 future头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.5" data-path="../appendix_D/D.5-chinese.html">
            
                <a href="../appendix_D/D.5-chinese.html">
            
                    
                    D.5 mutex头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.6" data-path="../appendix_D/D.6-chinese.html">
            
                <a href="../appendix_D/D.6-chinese.html">
            
                    
                    D.6 ratio头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.7" data-path="../appendix_D/D.7-chinese.html">
            
                <a href="../appendix_D/D.7-chinese.html">
            
                    
                    D.7 thread头文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >3.3 保护共享数据的方式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="33-&#x4FDD;&#x62A4;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x7684;&#x65B9;&#x5F0F;">3.3 &#x4FDD;&#x62A4;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x7684;&#x65B9;&#x5F0F;</h1>
<p>&#x4E92;&#x65A5;&#x91CF;&#x662F;&#x4E00;&#x79CD;&#x901A;&#x7528;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x4F46;&#x5176;&#x5E76;&#x975E;&#x4FDD;&#x62A4;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x7684;&#x552F;&#x4E00;&#x65B9;&#x5F0F;&#x3002;&#x6709;&#x5F88;&#x591A;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#x5728;&#x7279;&#x5B9A;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5BF9;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x63D0;&#x4F9B;&#x5408;&#x9002;&#x7684;&#x4FDD;&#x62A4;&#x3002;</p>
<p>&#x4E00;&#x4E2A;&#x7279;&#x522B;&#x6781;&#x7AEF;&#x7684;&#x60C5;&#x51B5;&#x5C31;&#x662F;&#xFF0C;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x5728;&#x5E76;&#x53D1;&#x8BBF;&#x95EE;&#x548C;&#x521D;&#x59CB;&#x5316;&#x65F6;(&#x90FD;&#x9700;&#x8981;&#x4FDD;&#x62A4;)&#xFF0C;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x9690;&#x5F0F;&#x540C;&#x6B65;&#x3002;&#x8FD9;&#x53EF;&#x80FD;&#x662F;&#x56E0;&#x4E3A;&#x6570;&#x636E;&#x4F5C;&#x4E3A;&#x53EA;&#x8BFB;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#xFF0C;&#x6240;&#x4EE5;&#x6CA1;&#x6709;&#x540C;&#x6B65;&#x95EE;&#x9898;&#xFF0C;&#x6216;&#x8005;&#x56E0;&#x4E3A;&#x5FC5;&#x8981;&#x7684;&#x4FDD;&#x62A4;&#x4F5C;&#x4E3A;&#x5BF9;&#x6570;&#x636E;&#x64CD;&#x4F5C;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x3002;&#x4EFB;&#x4F55;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6570;&#x636E;&#x521D;&#x59CB;&#x5316;&#x540E;&#x9501;&#x4F4F;&#x4E00;&#x4E2A;&#x4E92;&#x65A5;&#x91CF;&#xFF0C;&#x7EAF;&#x7CB9;&#x662F;&#x4E3A;&#x4E86;&#x4FDD;&#x62A4;&#x5176;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#xFF0C;&#x5E76;&#x4E14;&#x4F1A;&#x7ED9;&#x6027;&#x80FD;&#x5E26;&#x6765;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x5F71;&#x54CD;&#x3002;</p>
<p>&#x51FA;&#x4E8E;&#x4EE5;&#x4E0A;&#x7684;&#x539F;&#x56E0;&#xFF0C;C++&#x6807;&#x51C6;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x7EAF;&#x7CB9;&#x4FDD;&#x62A4;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#x7684;&#x673A;&#x5236;&#x3002;</p>
<h2 id="331-&#x4FDD;&#x62A4;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x7684;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;">3.3.1 &#x4FDD;&#x62A4;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x7684;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;</h2>
<p>&#x5047;&#x8BBE;&#x6709;&#x4E00;&#x4E2A;&#x5171;&#x4EAB;&#x6E90;&#xFF0C;&#x6784;&#x5EFA;&#x4EE3;&#x4EF7;&#x5F88;&#x6602;&#x8D35;&#xFF0C;&#x5B83;&#x53EF;&#x80FD;&#x4F1A;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;&#x6216;&#x5206;&#x914D;&#x51FA;&#x5F88;&#x591A;&#x7684;&#x5185;&#x5B58;&#x3002;</p>
<p>&#x5EF6;&#x8FDF;&#x521D;&#x59CB;&#x5316;(Lazy initialization)&#x5728;&#x5355;&#x7EBF;&#x7A0B;&#x4EE3;&#x7801;&#x5F88;&#x5E38;&#x89C1;&#x2014;&#x2014;&#x6BCF;&#x4E00;&#x4E2A;&#x64CD;&#x4F5C;&#x90FD;&#x9700;&#x8981;&#x5148;&#x5BF9;&#x6E90;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#xFF0C;&#x4E3A;&#x4E86;&#x4E86;&#x89E3;&#x6570;&#x636E;&#x662F;&#x5426;&#x88AB;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x5176;&#x4F7F;&#x7528;&#x524D;&#x51B3;&#x5B9A;&#xFF0C;&#x6570;&#x636E;&#x662F;&#x5426;&#x9700;&#x8981;&#x521D;&#x59CB;&#x5316;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">shared_ptr</span>&lt;some_resource&gt; resource_ptr;
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">foo</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-keyword">if</span>(!resource_ptr)
  {
    resource_ptr.reset(<span class="hljs-keyword">new</span> some_resource);  <span class="hljs-comment">// 1</span>
  }
  resource_ptr-&gt;do_something();
}
</code></pre>
<p>&#x8F6C;&#x4E3A;&#x591A;&#x7EBF;&#x7A0B;&#x4EE3;&#x7801;&#x65F6;&#xFF0C;&#x53EA;&#x6709;&#x2460;&#x5904;&#x9700;&#x8981;&#x4FDD;&#x62A4;&#xFF0C;&#x8FD9;&#x6837;&#x5171;&#x4EAB;&#x6570;&#x636E;&#x5BF9;&#x4E8E;&#x5E76;&#x53D1;&#x8BBF;&#x95EE;&#x5C31;&#x662F;&#x5B89;&#x5168;&#x7684;&#x3002;&#x4F46;&#x662F;&#x4E0B;&#x9762;&#x5929;&#x771F;&#x7684;&#x8F6C;&#x6362;&#x4F1A;&#x4F7F;&#x5F97;&#x7EBF;&#x7A0B;&#x8D44;&#x6E90;&#x4EA7;&#x751F;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x4E3A;&#x4E86;&#x786E;&#x5B9A;&#x6570;&#x636E;&#x6E90;&#x5DF2;&#x7ECF;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x5FC5;&#x987B;&#x7B49;&#x5F85;&#x4E92;&#x65A5;&#x91CF;&#x3002;</p>
<p>&#x4EE3;&#x7801;3.11 &#x4F7F;&#x7528;&#x5EF6;&#x8FDF;&#x521D;&#x59CB;&#x5316;(&#x7EBF;&#x7A0B;&#x5B89;&#x5168;)&#x7684;&#x8FC7;&#x7A0B;</p>
<pre><code class="lang-c++"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">shared_ptr</span>&lt;some_resource&gt; resource_ptr;
<span class="hljs-built_in">std</span>::mutex resource_mutex;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">foo</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">std</span>::unique_lock&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(resource_mutex);  <span class="hljs-comment">// &#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x5728;&#x6B64;&#x5E8F;&#x5217;&#x5316; </span>
  <span class="hljs-keyword">if</span>(!resource_ptr)
  {
    resource_ptr.reset(<span class="hljs-keyword">new</span> some_resource);  <span class="hljs-comment">// &#x53EA;&#x6709;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#x9700;&#x8981;&#x4FDD;&#x62A4; </span>
  }
  lk.unlock();
  resource_ptr-&gt;do_something();
}
</code></pre>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x76F8;&#x5F53;&#x5E38;&#x89C1;&#x4E86;&#xFF0C;&#x4E5F;&#x8DB3;&#x591F;&#x8868;&#x73B0;&#x51FA;&#x6CA1;&#x5FC5;&#x8981;&#x7684;&#x7EBF;&#x7A0B;&#x5316;&#x95EE;&#x9898;&#xFF0C;&#x5F88;&#x591A;&#x4EBA;&#x80FD;&#x60F3;&#x51FA;&#x66F4;&#x597D;&#x7684;&#x4E00;&#x4E9B;&#x7684;&#x529E;&#x6CD5;&#x6765;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#xFF0C;&#x5305;&#x62EC;&#x58F0;&#x540D;&#x72FC;&#x85C9;&#x7684;&#x201C;&#x53CC;&#x91CD;&#x68C0;&#x67E5;&#x9501;&#x6A21;&#x5F0F;&#x201D;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">undefined_behaviour_with_double_checked_locking</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-keyword">if</span>(!resource_ptr)  <span class="hljs-comment">// 1</span>
  {
    <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(resource_mutex);
    <span class="hljs-keyword">if</span>(!resource_ptr)  <span class="hljs-comment">// 2</span>
    {
      resource_ptr.reset(<span class="hljs-keyword">new</span> some_resource);  <span class="hljs-comment">// 3</span>
    }
  }
  resource_ptr-&gt;do_something();  <span class="hljs-comment">// 4</span>
}
</code></pre>
<p>&#x6307;&#x9488;&#x7B2C;&#x4E00;&#x6B21;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x4E0D;&#x9700;&#x8981;&#x83B7;&#x53D6;&#x9501;&#x2460;&#xFF0C;&#x5E76;&#x4E14;&#x53EA;&#x6709;&#x5728;&#x6307;&#x9488;&#x4E3A;&#x7A7A;&#x65F6;&#x624D;&#x9700;&#x8981;&#x83B7;&#x53D6;&#x9501;&#x3002;&#x7136;&#x540E;&#xFF0C;&#x5F53;&#x83B7;&#x53D6;&#x9501;&#x4E4B;&#x540E;&#xFF0C;&#x4F1A;&#x518D;&#x68C0;&#x67E5;&#x4E00;&#x6B21;&#x6307;&#x9488;&#x2461; (&#x8FD9;&#x5C31;&#x662F;&#x53CC;&#x91CD;&#x68C0;&#x67E5;&#x7684;&#x90E8;&#x5206;)&#xFF0C;&#x907F;&#x514D;&#x53E6;&#x4E00;&#x7EBF;&#x7A0B;&#x5728;&#x7B2C;&#x4E00;&#x6B21;&#x68C0;&#x67E5;&#x540E;&#x518D;&#x505A;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x5E76;&#x4E14;&#x8BA9;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x83B7;&#x53D6;&#x9501;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x6A21;&#x5F0F;&#x4E3A;&#x4EC0;&#x4E48;&#x58F0;&#x540D;&#x72FC;&#x85C9;&#x5462;&#xFF1F;&#x56E0;&#x4E3A;&#x6709;&#x6F5C;&#x5728;&#x7684;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#x3002;&#x672A;&#x88AB;&#x9501;&#x4FDD;&#x62A4;&#x7684;&#x8BFB;&#x53D6;&#x64CD;&#x4F5C;&#x2460;&#x6CA1;&#x6709;&#x4E0E;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x91CC;&#x88AB;&#x9501;&#x4FDD;&#x62A4;&#x7684;&#x5199;&#x5165;&#x64CD;&#x4F5C;&#x2462;&#x8FDB;&#x884C;&#x540C;&#x6B65;&#xFF0C;&#x56E0;&#x6B64;&#x5C31;&#x4F1A;&#x4EA7;&#x751F;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#xFF0C;&#x8FD9;&#x4E2A;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#x4E0D;&#x4EC5;&#x8986;&#x76D6;&#x6307;&#x9488;&#x672C;&#x8EAB;&#xFF0C;&#x8FD8;&#x4F1A;&#x5F71;&#x54CD;&#x5230;&#x5176;&#x6307;&#x5411;&#x7684;&#x5BF9;&#x8C61;&#xFF1B;&#x5373;&#x4F7F;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x77E5;&#x9053;&#x53E6;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x5BF9;&#x6307;&#x9488;&#x8FDB;&#x884C;&#x5199;&#x5165;&#xFF0C;&#x5B83;&#x53EF;&#x80FD;&#x6CA1;&#x6709;&#x770B;&#x5230;&#x65B0;&#x521B;&#x5EFA;&#x7684;some_resource&#x5B9E;&#x4F8B;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;do_something()&#x2463;&#x540E;&#xFF0C;&#x5F97;&#x5230;&#x4E0D;&#x6B63;&#x786E;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x662F;&#x5728;&#x4E00;&#x79CD;&#x5178;&#x578B;&#x7684;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#x2014;&#x2014;&#x6570;&#x636E;&#x7ADE;&#x4E89;&#xFF0C;C++&#x6807;&#x51C6;&#x4E2D;&#x6307;&#x5B9A;&#x4E3A;&#x201C;&#x672A;&#x5B9A;&#x4E49;&#x884C;&#x4E3A;&#x201D;&#xFF0C;&#x8FD9;&#x79CD;&#x7ADE;&#x4E89;&#x662F;&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x7684;&#x3002;&#x9605;&#x8BFB;&#x7B2C;5&#x7AE0;&#x65F6;&#xFF0C;&#x90A3;&#x91CC;&#x6709;&#x66F4;&#x591A;&#x5BF9;&#x5185;&#x5B58;&#x6A21;&#x578B;&#x7684;&#x8BA8;&#x8BBA;&#xFF0C;&#x4E5F;&#x5305;&#x62EC;&#x6570;&#x636E;&#x7ADE;&#x4E89;&#x7684;&#x6784;&#x6210;&#x3002;(&#x8BD1;&#x8005;&#x6CE8;&#xFF1A;&#x8457;&#x540D;&#x7684;&#x300A;C++&#x548C;&#x53CC;&#x91CD;&#x68C0;&#x67E5;&#x9501;&#x5B9A;&#x6A21;&#x5F0F;(DCLP)&#x7684;&#x98CE;&#x9669;&#x300B;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x8865;&#x5145;&#x6750;&#x6599;&#x4F9B;&#x5927;&#x5BB6;&#x53C2;&#x8003; <a href="https://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf" target="_blank">&#x82F1;&#x6587;&#x7248;</a> <a href="http://blog.jobbole.com/86392/" target="_blank">&#x4E2D;&#x6587;&#x7248;</a>)</p>
<p>C++&#x6807;&#x51C6;&#x59D4;&#x5458;&#x4F1A;&#x4E5F;&#x8BA4;&#x4E3A;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#x7684;&#x5904;&#x7406;&#x5F88;&#x91CD;&#x8981;&#xFF0C;&#x6240;&#x4EE5;C++&#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x4E86;<code>std::once_flag</code>&#x548C;<code>std::call_once</code>&#x6765;&#x5904;&#x7406;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x3002;&#x6BD4;&#x8D77;&#x9501;&#x4F4F;&#x4E92;&#x65A5;&#x91CF;&#x5E76;&#x663E;&#x5F0F;&#x7684;&#x68C0;&#x67E5;&#x6307;&#x9488;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x53EA;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>std::call_once</code>&#x5C31;&#x53EF;&#x4EE5;&#xFF0C;&#x5728;<code>std::call_once</code>&#x7684;&#x7ED3;&#x675F;&#x65F6;&#xFF0C;&#x5C31;&#x80FD;&#x5B89;&#x5168;&#x7684;&#x77E5;&#x6653;&#x6307;&#x9488;&#x5DF2;&#x7ECF;&#x88AB;&#x5176;&#x4ED6;&#x7684;&#x7EBF;&#x7A0B;&#x521D;&#x59CB;&#x5316;&#x4E86;&#x3002;&#x4F7F;&#x7528;<code>std::call_once</code>&#x6BD4;&#x663E;&#x5F0F;&#x4F7F;&#x7528;&#x4E92;&#x65A5;&#x91CF;&#x6D88;&#x8017;&#x7684;&#x8D44;&#x6E90;&#x66F4;&#x5C11;&#xFF0C;&#x7279;&#x522B;&#x662F;&#x5F53;&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6210;&#x540E;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5C55;&#x793A;&#x4E86;&#x4E0E;&#x4EE3;&#x7801;3.11&#x4E2D;&#x7684;&#x540C;&#x6837;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x4E86;<code>std::call_once</code>&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x5B8C;&#x6210;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x64CD;&#x4F5C;&#x4F7F;&#x7528;&#x7C7B;&#x4E2D;&#x7684;&#x51FD;&#x6570;&#x64CD;&#x4F5C;&#x7B26;&#x6765;&#x5B9E;&#x73B0;&#x540C;&#x6837;&#x5F88;&#x7B80;&#x5355;&#x3002;&#x5982;&#x540C;&#x5927;&#x591A;&#x6570;&#x5728;&#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x7684;&#x51FD;&#x6570;&#x4E00;&#x6837;&#xFF0C;&#x6216;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x6216;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x88AB;&#x4F20;&#x9012;&#xFF0C;<code>std::call_once</code>&#x53EF;&#x4EE5;&#x548C;&#x4EFB;&#x4F55;&#x51FD;&#x6570;&#x6216;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#x3002;</p>
<pre><code class="lang-c++"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">shared_ptr</span>&lt;some_resource&gt; resource_ptr;
<span class="hljs-built_in">std</span>::once_flag resource_flag;  <span class="hljs-comment">// 1</span>

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">init_resource</span><span class="hljs-params">()</span>
</span>{
  resource_ptr.reset(<span class="hljs-keyword">new</span> some_resource);
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">foo</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">std</span>::call_once(resource_flag,init_resource);  <span class="hljs-comment">// &#x53EF;&#x4EE5;&#x5B8C;&#x6574;&#x7684;&#x8FDB;&#x884C;&#x4E00;&#x6B21;&#x521D;&#x59CB;&#x5316;</span>
  resource_ptr-&gt;do_something();
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>std::once_flag</code>&#x2460;&#x548C;&#x521D;&#x59CB;&#x5316;&#x597D;&#x7684;&#x6570;&#x636E;&#x90FD;&#x662F;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x533A;&#x57DF;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4F46;<code>std::call_once()</code>&#x53EF;&#x4EC5;&#x4F5C;&#x4E3A;&#x5EF6;&#x8FDF;&#x521D;&#x59CB;&#x5316;&#x7684;&#x7C7B;&#x578B;&#x6210;&#x5458;&#xFF0C;&#x5982;&#x540C;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E00;&#x6837;&#xFF1A;</p>
<p>&#x4EE3;&#x7801;3.12 &#x4F7F;&#x7528;<code>std::call_once</code>&#x4F5C;&#x4E3A;&#x7C7B;&#x6210;&#x5458;&#x7684;&#x5EF6;&#x8FDF;&#x521D;&#x59CB;&#x5316;(&#x7EBF;&#x7A0B;&#x5B89;&#x5168;)</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> X
{
<span class="hljs-keyword">private</span>:
  connection_info connection_details;
  connection_handle connection;
  <span class="hljs-built_in">std</span>::once_flag connection_init_flag;

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">open_connection</span><span class="hljs-params">()</span>
  </span>{
    connection=connection_manager.open(connection_details);
  }
<span class="hljs-keyword">public</span>:
  X(connection_info <span class="hljs-keyword">const</span>&amp; connection_details_):
      connection_details(connection_details_)
  {}
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">send_data</span><span class="hljs-params">(data_packet <span class="hljs-keyword">const</span>&amp; data)</span>  <span class="hljs-comment">// 1</span>
  </span>{
    <span class="hljs-built_in">std</span>::call_once(connection_init_flag,&amp;X::open_connection,<span class="hljs-keyword">this</span>);  <span class="hljs-comment">// 2</span>
    connection.send_data(data);
  }
  <span class="hljs-function">data_packet <span class="hljs-title">receive_data</span><span class="hljs-params">()</span>  <span class="hljs-comment">// 3</span>
  </span>{
    <span class="hljs-built_in">std</span>::call_once(connection_init_flag,&amp;X::open_connection,<span class="hljs-keyword">this</span>);  <span class="hljs-comment">// 2</span>
    <span class="hljs-keyword">return</span> connection.receive_data();
  }
};
</code></pre>
<p>&#x4F8B;&#x5B50;&#x4E2D;&#x7B2C;&#x4E00;&#x6B21;&#x8C03;&#x7528;send_data()&#x2460;&#x6216;receive_data()&#x2462;&#x7684;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#x3002;&#x4F7F;&#x7528;&#x6210;&#x5458;&#x51FD;&#x6570;open_connection()&#x53BB;&#x521D;&#x59CB;&#x5316;&#x6570;&#x636E;&#xFF0C;&#x4E5F;&#x9700;&#x8981;&#x5C06;this&#x6307;&#x9488;&#x4F20;&#x8FDB;&#x53BB;&#x3002;&#x548C;&#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x7684;&#x51FD;&#x6570;&#x4E00;&#x6837;&#xFF0C;&#x63A5;&#x53D7;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#xFF0C;&#x6BD4;&#x5982;<code>std::thread</code>&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;<code>std::bind()</code>&#xFF0C;&#x901A;&#x8FC7;&#x5411;<code>std::call_once()</code>&#x2461;&#x4F20;&#x9012;&#x4E00;&#x4E2A;&#x989D;&#x5916;&#x7684;&#x53C2;&#x6570;&#x6765;&#x5B8C;&#x6210;&#x8FD9;&#x4E2A;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x503C;&#x5F97;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;<code>std::mutex</code>&#x548C;<code>std::once_flag</code>&#x7684;&#x5B9E;&#x4F8B;&#x4E0D;&#x80FD;&#x62F7;&#x8D1D;&#x548C;&#x79FB;&#x52A8;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x663E;&#x5F0F;&#x5B9A;&#x4E49;&#x76F8;&#x5E94;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x8FD9;&#x4E9B;&#x7C7B;&#x6210;&#x5458;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x8FD8;&#x6709;&#x4E00;&#x79CD;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#x4E2D;&#x6F5C;&#x5B58;&#x7740;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#xFF1A;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x4E3A;static&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x79CD;&#x53D8;&#x91CF;&#x7684;&#x5728;&#x58F0;&#x660E;&#x540E;&#x5C31;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#x521D;&#x59CB;&#x5316;&#x3002;&#x5BF9;&#x4E8E;&#x591A;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x5C31;&#x610F;&#x5473;&#x7740;&#x8FD9;&#x91CC;&#x6709;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#x2014;&#x2014;&#x62A2;&#x7740;&#x53BB;&#x5B9A;&#x4E49;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x3002;&#x5F88;&#x591A;&#x5728;&#x4E0D;&#x652F;&#x6301;C++11&#x6807;&#x51C6;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x4E0A;&#xFF0C;&#x5728;&#x5B9E;&#x8DF5;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#x662F;&#x786E;&#x5B9E;&#x5B58;&#x5728;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x4E2D;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x8BA4;&#x4E3A;&#x4ED6;&#x4EEC;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x521D;&#x59CB;&#x5316;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x7EBF;&#x7A0B;&#xFF0C;&#x6216;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5BF9;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x800C;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8981;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;&#x8FD8;&#x6CA1;&#x5B8C;&#x6210;&#x3002;&#x5728;C++11&#x6807;&#x51C6;&#x4E2D;&#xFF0C;&#x8FD9;&#x4E9B;&#x95EE;&#x9898;&#x90FD;&#x88AB;&#x89E3;&#x51B3;&#x4E86;&#xFF1A;&#x521D;&#x59CB;&#x5316;&#x53CA;&#x5B9A;&#x4E49;&#x5B8C;&#x5168;&#x5728;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x53D1;&#x751F;&#xFF0C;&#x5E76;&#x4E14;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x53EF;&#x5728;&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6210;&#x524D;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x6761;&#x4EF6;&#x7ADE;&#x4E89;&#x7EC8;&#x6B62;&#x4E8E;&#x521D;&#x59CB;&#x5316;&#x9636;&#x6BB5;&#xFF0C;&#x8FD9;&#x6837;&#x6BD4;&#x5728;&#x4E4B;&#x540E;&#x518D;&#x53BB;&#x5904;&#x7406;&#x597D;&#x7684;&#x591A;&#x3002;&#x5728;&#x53EA;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x5B9E;&#x4F8B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8FD9;&#x91CC;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;<code>std::call_once</code>&#x7684;&#x66FF;&#x4EE3;&#x65B9;&#x6848;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> my_class;
<span class="hljs-function">my_class&amp; <span class="hljs-title">get_my_class_instance</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-keyword">static</span> my_class instance;  <span class="hljs-comment">// &#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x7684;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7A0B;</span>
  <span class="hljs-keyword">return</span> instance;
}
</code></pre>
<p>&#x591A;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x5B89;&#x5168;&#x7684;&#x8C03;&#x7528;get_my_class_instance()&#x2460;&#x51FD;&#x6570;&#xFF0C;&#x4E0D;&#x7528;&#x4E3A;&#x6570;&#x636E;&#x7ADE;&#x4E89;&#x800C;&#x62C5;&#x5FC3;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x5F88;&#x5C11;&#x6709;&#x66F4;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x6765;&#x8BF4;&#xFF0C;&#x53EA;&#x5728;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x4FDD;&#x62A4;&#x6570;&#x636E;&#x3002;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8FD9;&#x79CD;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x662F;&#x53EA;&#x8BFB;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x591A;&#x7EBF;&#x7A0B;&#x5BF9;&#x5176;&#x5E76;&#x53D1;&#x7684;&#x8BFB;&#x53D6;&#x4E5F;&#x662F;&#x5F88;&#x6109;&#x5FEB;&#x7684;&#xFF0C;&#x4E0D;&#x8FC7;&#x4E00;&#x65E6;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x9700;&#x8981;&#x66F4;&#x65B0;&#x5C31;&#x4F1A;&#x4EA7;&#x751F;&#x7ADE;&#x4E89;&#x3002;</p>
<h2 id="332-&#x4FDD;&#x62A4;&#x4E0D;&#x5E38;&#x66F4;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;">3.3.2 &#x4FDD;&#x62A4;&#x4E0D;&#x5E38;&#x66F4;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;</h2>
<p>&#x8BD5;&#x60F3;&#x4E3A;&#x4E86;&#x5C06;&#x57DF;&#x540D;&#x89E3;&#x6790;&#x4E3A;&#x5176;&#x76F8;&#x5173;IP&#x5730;&#x5740;&#xFF0C;&#x5728;&#x7F13;&#x5B58;&#x4E2D;&#x7684;&#x5B58;&#x653E;&#x4E86;&#x4E00;&#x5F20;DNS&#x5165;&#x53E3;&#x8868;&#x3002;&#x901A;&#x5E38;&#xFF0C;&#x7ED9;&#x5B9A;DNS&#x6570;&#x76EE;&#x5728;&#x5F88;&#x957F;&#x7684;&#x65F6;&#x95F4;&#x5185;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;&#x3002;&#x867D;&#x7136;&#xFF0C;&#x7528;&#x6237;&#x8BBF;&#x95EE;&#x4E0D;&#x540C;&#x7F51;&#x7AD9;&#x65F6;&#xFF0C;&#x65B0;&#x7684;&#x5165;&#x53E3;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x6DFB;&#x52A0;&#x5230;&#x8868;&#x4E2D;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x53EF;&#x80FD;&#x5728;&#x5176;&#x751F;&#x547D;&#x5468;&#x671F;&#x5185;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;&#x3002;&#x6240;&#x4EE5;&#x5B9A;&#x671F;&#x68C0;&#x67E5;&#x7F13;&#x5B58;&#x4E2D;&#x5165;&#x53E3;&#x7684;&#x6709;&#x6548;&#x6027;&#x5C31;&#x53D8;&#x7684;&#x5341;&#x5206;&#x91CD;&#x8981;&#x3002;&#x4F46;&#x4E5F;&#x9700;&#x8981;&#x4E00;&#x6B21;&#x66F4;&#x65B0;&#xFF0C;&#x4E5F;&#x8BB8;&#x8FD9;&#x6B21;&#x66F4;&#x65B0;&#x53EA;&#x662F;&#x5BF9;&#x4E00;&#x4E9B;&#x7EC6;&#x8282;&#x505A;&#x4E86;&#x6539;&#x52A8;&#x3002;</p>
<p>&#x867D;&#x7136;&#x66F4;&#x65B0;&#x9891;&#x5EA6;&#x5F88;&#x4F4E;&#xFF0C;&#x4F46;&#x4E5F;&#x6709;&#x53EF;&#x80FD;&#x53D1;&#x751F;&#xFF0C;&#x5E76;&#x4E14;&#x5F53;&#x7F13;&#x5B58;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x7F13;&#x5B58;&#x5C31;&#x9700;&#x8981;&#x4FDD;&#x62A4;&#x66F4;&#x65B0;&#x65F6;&#x72B6;&#x6001;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x4E5F;&#x662F;&#x4E3A;&#x4E86;&#x786E;&#x4FDD;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BFB;&#x5230;&#x90FD;&#x662F;&#x6709;&#x6548;&#x6570;&#x636E;&#x3002;</p>
<p>&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x4E13;&#x7528;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x65F6;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x7B26;&#x5408;&#x9884;&#x671F;&#x7684;&#xFF0C;&#x5E76;&#x4E3A;&#x5E76;&#x53D1;&#x66F4;&#x65B0;&#x548C;&#x8BFB;&#x53D6;&#x8FDB;&#x884C;&#x4E86;&#x7279;&#x522B;&#x8BBE;&#x8BA1;(&#x66F4;&#x591A;&#x7684;&#x4F8B;&#x5B50;&#x5728;&#x7B2C;6&#x548C;&#x7B2C;7&#x7AE0;&#x4E2D;&#x4ECB;&#x7ECD;)&#x3002;&#x8FD9;&#x6837;&#x7684;&#x66F4;&#x65B0;&#x8981;&#x6C42;&#x7EBF;&#x7A0B;&#x72EC;&#x5360;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x8BBF;&#x95EE;&#x6743;&#xFF0C;&#x76F4;&#x5230;&#x66F4;&#x65B0;&#x64CD;&#x4F5C;&#x5B8C;&#x6210;&#x3002;&#x5F53;&#x5B8C;&#x6210;&#x66F4;&#x65B0;&#x65F6;&#xFF0C;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x5BF9;&#x4E8E;&#x5E76;&#x53D1;&#x591A;&#x7EBF;&#x7A0B;&#x7684;&#x8BBF;&#x95EE;&#x53C8;&#x4F1A;&#x662F;&#x5B89;&#x5168;&#x7684;&#x3002;&#x4F7F;&#x7528;<code>std::mutex</code>&#x6765;&#x4FDD;&#x62A4;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x611F;&#x89C9;&#x6709;&#x4E9B;&#x53CD;&#x5E94;&#x8FC7;&#x5EA6;(&#x56E0;&#x4E3A;&#x5728;&#x6CA1;&#x6709;&#x53D1;&#x751F;&#x4FEE;&#x6539;&#x65F6;&#xFF0C;&#x5B83;&#x5C06;&#x524A;&#x51CF;&#x5E76;&#x53D1;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x7684;&#x53EF;&#x80FD;&#x6027;)&#x3002;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x53E6;&#x4E00;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x4E92;&#x65A5;&#x91CF;&#xFF0C;&#x8FD9;&#x79CD;&#x4E92;&#x65A5;&#x91CF;&#x5E38;&#x88AB;&#x79F0;&#x4E3A;&#x201C;&#x8BFB;&#x8005;-&#x4F5C;&#x8005;&#x9501;&#x201D;&#xFF0C;&#x56E0;&#x4E3A;&#x5176;&#x5141;&#x8BB8;&#x4E24;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#xFF1A;&#x4E00;&#x4E2A;&#x201C;&#x4F5C;&#x8005;&#x201D;&#x7EBF;&#x7A0B;&#x72EC;&#x5360;&#x8BBF;&#x95EE;&#x548C;&#x5171;&#x4EAB;&#x8BBF;&#x95EE;&#xFF0C;&#x8BA9;&#x591A;&#x4E2A;&#x201C;&#x8BFB;&#x8005;&#x201D;&#x7EBF;&#x7A0B;&#x5E76;&#x53D1;&#x8BBF;&#x95EE;&#x3002;</p>
<p>C++17&#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x79CD;&#x975E;&#x5E38;&#x597D;&#x7684;&#x4E92;&#x65A5;&#x91CF;&#x2014;&#x2014;<code>std::shared_mutex</code>&#x548C;<code>std::shared_timed_mutex</code>&#x3002;C++14&#x53EA;&#x63D0;&#x4F9B;&#x4E86;<code>std::shared_timed_mutex</code>&#xFF0C;&#x5E76;&#x4E14;&#x5728;C++11&#x4E2D;&#x5E76;&#x672A;&#x63D0;&#x4F9B;&#x4EFB;&#x4F55;&#x4E92;&#x65A5;&#x91CF;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x8FD8;&#x5728;&#x7528;&#x652F;&#x6301;C++14&#x6807;&#x51C6;&#x4E4B;&#x524D;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;Boost&#x5E93;&#x4E2D;&#x7684;&#x4E92;&#x65A5;&#x91CF;&#x3002;<code>std::shared_mutex</code>&#x548C;<code>std::shared_timed_mutex</code>&#x7684;&#x4E0D;&#x540C;&#x70B9;&#x5728;&#x4E8E;&#xFF0C;<code>std::shared_timed_mutex</code>&#x652F;&#x6301;&#x66F4;&#x591A;&#x7684;&#x64CD;&#x4F5C;&#x65B9;&#x5F0F;(&#x53C2;&#x8003;4.3&#x8282;)&#xFF0C;<code>std::shared_mutex</code>&#x6709;&#x66F4;&#x9AD8;&#x7684;&#x6027;&#x80FD;&#x4F18;&#x52BF;&#xFF0C;&#x4F46;&#x652F;&#x6301;&#x7684;&#x64CD;&#x4F5C;&#x8F83;&#x5C11;&#x3002;</p>
<p>&#x7B2C;8&#x7AE0;&#x4E2D;&#x4F1A;&#x770B;&#x5230;&#xFF0C;&#x8FD9;&#x79CD;&#x9501;&#x7684;&#x4E5F;&#x4E0D;&#x80FD;&#x5305;&#x6CBB;&#x767E;&#x75C5;&#xFF0C;&#x5176;&#x6027;&#x80FD;&#x4F9D;&#x8D56;&#x4E8E;&#x53C2;&#x4E0E;&#x5176;&#x4E2D;&#x7684;&#x5904;&#x7406;&#x5668;&#x6570;&#x91CF;&#xFF0C;&#x540C;&#x6837;&#x4E5F;&#x4E0E;&#x8BFB;&#x8005;&#x548C;&#x4F5C;&#x8005;&#x7EBF;&#x7A0B;&#x7684;&#x8D1F;&#x8F7D;&#x6709;&#x5173;&#x3002;&#x4E3A;&#x4E86;&#x786E;&#x4FDD;&#x589E;&#x52A0;&#x590D;&#x6742;&#x5EA6;&#x540E;&#x8FD8;&#x80FD;&#x83B7;&#x5F97;&#x6027;&#x80FD;&#x6536;&#x76CA;&#xFF0C;&#x76EE;&#x6807;&#x7CFB;&#x7EDF;&#x4E0A;&#x7684;&#x4EE3;&#x7801;&#x6027;&#x80FD;&#x5C31;&#x5F88;&#x91CD;&#x8981;&#x3002;</p>
<p>&#x6BD4;&#x8D77;&#x4F7F;&#x7528;<code>std::mutex</code>&#x5B9E;&#x4F8B;&#x8FDB;&#x884C;&#x540C;&#x6B65;&#xFF0C;&#x4E0D;&#x5982;&#x4F7F;&#x7528;<code>std::shared_mutex</code>&#x6765;&#x505A;&#x540C;&#x6B65;&#x3002;&#x5BF9;&#x4E8E;&#x66F4;&#x65B0;&#x64CD;&#x4F5C;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>std::lock_guard&lt;std::shared_mutex&gt;</code>&#x548C;<code>std::unique_lock&lt;std::shared_mutex&gt;</code>&#x4E0A;&#x9501;&#x3002;&#x4F5C;&#x4E3A;<code>std::mutex</code>&#x7684;&#x66FF;&#x4EE3;&#x65B9;&#x6848;&#xFF0C;&#x4E0E;<code>std::mutex</code>&#x6240;&#x505A;&#x7684;&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x5C31;&#x80FD;&#x4FDD;&#x8BC1;&#x66F4;&#x65B0;&#x7EBF;&#x7A0B;&#x7684;&#x72EC;&#x5360;&#x8BBF;&#x95EE;&#x3002;&#x90A3;&#x4E9B;&#x65E0;&#x9700;&#x4FEE;&#x6539;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>std::shared_lock&lt;std::shared_mutex&gt;</code>&#x83B7;&#x53D6;&#x8BBF;&#x95EE;&#x6743;&#x3002;&#x8FD9;&#x79CD;RAII&#x7C7B;&#x578B;&#x6A21;&#x677F;&#x662F;&#x5728;C++14&#x4E2D;&#x7684;&#x65B0;&#x7279;&#x6027;&#xFF0C;&#x8FD9;&#x4E0E;&#x4F7F;&#x7528;<code>std::unique_lock</code>&#x4E00;&#x6837;&#xFF0C;&#x9664;&#x4E86;&#x591A;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x83B7;&#x53D6;&#x540C;&#x4E00;&#x4E2A;<code>std::shared_mutex</code>&#x7684;&#x5171;&#x4EAB;&#x9501;&#x3002;&#x552F;&#x4E00;&#x7684;&#x9650;&#x5236;&#xFF1A;&#x5F53;&#x6709;&#x7EBF;&#x7A0B;&#x62E5;&#x6709;&#x5171;&#x4EAB;&#x9501;&#x65F6;&#xFF0C;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x72EC;&#x5360;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#x4F1A;&#x88AB;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x6240;&#x6709;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x653E;&#x5F03;&#x9501;&#x3002;&#x5F53;&#x4EFB;&#x4E00;&#x7EBF;&#x7A0B;&#x62E5;&#x6709;&#x4E00;&#x4E2A;&#x72EC;&#x5360;&#x9501;&#x65F6;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5C31;&#x65E0;&#x6CD5;&#x83B7;&#x5F97;&#x5171;&#x4EAB;&#x9501;&#x6216;&#x72EC;&#x5360;&#x9501;&#xFF0C;&#x76F4;&#x5230;&#x7B2C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x653E;&#x5F03;&#x5176;&#x62E5;&#x6709;&#x7684;&#x9501;&#x3002;</p>
<p>&#x5982;&#x540C;&#x4E4B;&#x524D;&#x63CF;&#x8FF0;&#x7684;&#x90A3;&#x6837;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x6E05;&#x5355;&#x5C55;&#x793A;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;DNS&#x7F13;&#x5B58;&#xFF0C;&#x4F7F;&#x7528;<code>std::map</code>&#x6301;&#x6709;&#x7F13;&#x5B58;&#x6570;&#x636E;&#xFF0C;&#x4F7F;&#x7528;<code>std::shared_mutex</code>&#x8FDB;&#x884C;&#x4FDD;&#x62A4;&#x3002;</p>
<p>&#x4EE3;&#x7801;3.13 &#x4F7F;&#x7528;<code>std::shared_mutex</code>&#x5BF9;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x8FDB;&#x884C;&#x4FDD;&#x62A4;</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;map&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;mutex&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;shared_mutex&gt;</span></span>

<span class="hljs-keyword">class</span> dns_entry;

<span class="hljs-keyword">class</span> dns_cache
{
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">map</span>&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span>,dns_entry&gt; entries;
  <span class="hljs-keyword">mutable</span> <span class="hljs-built_in">std</span>::shared_mutex entry_mutex;
<span class="hljs-keyword">public</span>:
  <span class="hljs-function">dns_entry <span class="hljs-title">find_entry</span><span class="hljs-params">(<span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> <span class="hljs-keyword">const</span>&amp; domain)</span> <span class="hljs-keyword">const</span>
  </span>{
    <span class="hljs-built_in">std</span>::shared_lock&lt;<span class="hljs-built_in">std</span>::shared_mutex&gt; lk(entry_mutex);  <span class="hljs-comment">// 1</span>
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">map</span>&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span>,dns_entry&gt;::const_iterator <span class="hljs-keyword">const</span> it=
       entries.find(domain);
    <span class="hljs-keyword">return</span> (it==entries.end())?dns_entry():it-&gt;second;
  }
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">update_or_add_entry</span><span class="hljs-params">(<span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> <span class="hljs-keyword">const</span>&amp; domain,
                           dns_entry <span class="hljs-keyword">const</span>&amp; dns_details)</span>
  </span>{
    <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::shared_mutex&gt; lk(entry_mutex);  <span class="hljs-comment">// 2</span>
    entries[domain]=dns_details;
  }
};
</code></pre>
<p>&#x4EE3;&#x7801;3.13&#x4E2D;&#xFF0C;find_entry()&#x4F7F;&#x7528;<code>std::shared_lock&lt;&gt;</code>&#x6765;&#x4FDD;&#x62A4;&#x5171;&#x4EAB;&#x548C;&#x53EA;&#x8BFB;&#x6743;&#x9650;&#x2460;&#x3002;&#x8FD9;&#x5C31;&#x4F7F;&#x5F97;&#x591A;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x8C03;&#x7528;find_entry()&#xFF0C;&#x4E14;&#x4E0D;&#x4F1A;&#x51FA;&#x9519;&#x3002;&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;update_or_add_entry()&#x4F7F;&#x7528;<code>std::lock_guard&lt;&gt;</code>&#x5B9E;&#x4F8B;&#xFF0C;&#x5F53;&#x8868;&#x683C;&#x9700;&#x8981;&#x66F4;&#x65B0;&#x65F6;&#x2461;&#xFF0C;&#x4E3A;&#x5176;&#x63D0;&#x4F9B;&#x72EC;&#x5360;&#x8BBF;&#x95EE;&#x6743;&#x9650;&#x3002;update_or_add_entry()&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x72EC;&#x5360;&#x9501;&#x4F1A;&#x963B;&#x6B62;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5BF9;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x5E76;&#x4E14;&#x963B;&#x6B62;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;find_entry()&#x3002;</p>
<h2 id="333-&#x5D4C;&#x5957;&#x9501;">3.3.3 &#x5D4C;&#x5957;&#x9501;</h2>
<p>&#x7EBF;&#x7A0B;&#x5BF9;&#x5DF2;&#x7ECF;&#x83B7;&#x53D6;&#x7684;<code>std::mutex</code>(&#x5DF2;&#x7ECF;&#x4E0A;&#x9501;)&#x518D;&#x6B21;&#x4E0A;&#x9501;&#x662F;&#x9519;&#x8BEF;&#x7684;&#xFF0C;&#x5C1D;&#x8BD5;&#x8FD9;&#x6837;&#x505A;&#x4F1A;&#x5BFC;&#x81F4;&#x672A;&#x5B9A;&#x4E49;&#x884C;&#x4E3A;&#x3002;&#x5728;&#x67D0;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4F1A;&#x5C1D;&#x8BD5;&#x5728;&#x91CA;&#x653E;&#x4E00;&#x4E2A;&#x4E92;&#x65A5;&#x91CF;&#x524D;&#x591A;&#x6B21;&#x83B7;&#x53D6;&#x3002;&#x56E0;&#x6B64;&#xFF0C;C++&#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x4E86;<code>std::recursive_mutex</code>&#x7C7B;&#x3002;&#x9664;&#x4E86;&#x53EF;&#x4EE5;&#x5728;&#x540C;&#x4E00;&#x7EBF;&#x7A0B;&#x7684;&#x5355;&#x4E2A;&#x5B9E;&#x4F8B;&#x4E0A;&#x591A;&#x6B21;&#x4E0A;&#x9501;&#xFF0C;&#x5176;&#x4ED6;&#x529F;&#x80FD;&#x4E0E;<code>std::mutex</code>&#x76F8;&#x540C;&#x3002;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5BF9;&#x4E92;&#x65A5;&#x91CF;&#x4E0A;&#x9501;&#x524D;&#xFF0C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5FC5;&#x987B;&#x91CA;&#x653E;&#x62E5;&#x6709;&#x7684;&#x6240;&#x6709;&#x9501;&#xFF0C;&#x6240;&#x4EE5;&#x5982;&#x679C;&#x4F60;&#x8C03;&#x7528;lock()&#x4E09;&#x6B21;&#xFF0C;&#x4E5F;&#x5FC5;&#x987B;&#x8C03;&#x7528;unlock()&#x4E09;&#x6B21;&#x3002;&#x6B63;&#x786E;&#x4F7F;&#x7528;<code>std::lock_guard&lt;std::recursive_mutex&gt;</code>&#x548C;<code>std::unique_lock&lt;std::recursive_mutex&gt;</code>&#x53EF;&#x4EE5;&#x5E2E;&#x4F60;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x5D4C;&#x5957;&#x9501;&#x65F6;&#xFF0C;&#x8981;&#x5BF9;&#x4EE3;&#x7801;&#x8BBE;&#x8BA1;&#x8FDB;&#x884C;&#x6539;&#x52A8;&#x3002;&#x5D4C;&#x5957;&#x9501;&#x4E00;&#x822C;&#x7528;&#x5728;&#x53EF;&#x5E76;&#x53D1;&#x8BBF;&#x95EE;&#x7684;&#x7C7B;&#x4E0A;&#xFF0C;&#x6240;&#x4EE5;&#x4F7F;&#x7528;&#x4E92;&#x65A5;&#x91CF;&#x4FDD;&#x62A4;&#x5176;&#x6210;&#x5458;&#x6570;&#x636E;&#x3002;&#x6BCF;&#x4E2A;&#x516C;&#x5171;&#x6210;&#x5458;&#x51FD;&#x6570;&#x90FD;&#x4F1A;&#x5BF9;&#x4E92;&#x65A5;&#x91CF;&#x4E0A;&#x9501;&#xFF0C;&#x7136;&#x540E;&#x5B8C;&#x6210;&#x5BF9;&#x5E94;&#x7684;&#x64CD;&#x4F5C;&#x540E;&#x518D;&#x89E3;&#x9501;&#x4E92;&#x65A5;&#x91CF;&#x3002;&#x4E0D;&#x8FC7;&#xFF0C;&#x6709;&#x65F6;&#x6210;&#x5458;&#x51FD;&#x6570;&#x4F1A;&#x8C03;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x6210;&#x5458;&#x51FD;&#x6570;&#x4E5F;&#x4F1A;&#x8BD5;&#x56FE;&#x9501;&#x4F4F;&#x4E92;&#x65A5;&#x91CF;&#xFF0C;&#x8FD9;&#x5C31;&#x4F1A;&#x5BFC;&#x81F4;&#x672A;&#x5B9A;&#x4E49;&#x884C;&#x4E3A;&#x7684;&#x53D1;&#x751F;&#x3002;&#x201C;&#x53D8;&#x901A;&#x7684;&#x201D;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x4F1A;&#x5C06;&#x4E92;&#x65A5;&#x91CF;&#x8F6C;&#x4E3A;&#x5D4C;&#x5957;&#x9501;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x6210;&#x5458;&#x51FD;&#x6570;&#x5C31;&#x80FD;&#x6210;&#x529F;&#x7684;&#x8FDB;&#x884C;&#x4E0A;&#x9501;&#xFF0C;&#x5E76;&#x4E14;&#x51FD;&#x6570;&#x80FD;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x3002;</p>
<p>&#x4F46;&#x662F;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x8FC7;&#x4E8E;&#x8349;&#x7387;&#x548C;&#x4E0D;&#x5408;&#x7406;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x63A8;&#x8350;&#x8FD9;&#x6837;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x3002;&#x7279;&#x522B;&#x662F;&#xFF0C;&#x5BF9;&#x5E94;&#x7C7B;&#x7684;&#x4E0D;&#x53D8;&#x91CF;&#x901A;&#x5E38;&#x4F1A;&#x88AB;&#x7834;&#x574F;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x5F53;&#x4E0D;&#x53D8;&#x91CF;&#x88AB;&#x7834;&#x574F;&#x65F6;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x6210;&#x5458;&#x51FD;&#x6570;&#x8FD8;&#x9700;&#x8981;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x3002;&#x4E00;&#x4E2A;&#x6BD4;&#x8F83;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#xFF0C;&#x4ECE;&#x4E2D;&#x63D0;&#x53D6;&#x51FA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x7C7B;&#x7684;&#x79C1;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x8FD9;&#x4E2A;&#x79C1;&#x6709;&#x6210;&#x5458;&#x51FD;&#x6570;&#x4E0D;&#x4F1A;&#x5BF9;&#x4E92;&#x65A5;&#x91CF;&#x8FDB;&#x884C;&#x4E0A;&#x9501;(&#x8C03;&#x7528;&#x524D;&#x5FC5;&#x987B;&#x83B7;&#x5F97;&#x9501;)&#x3002;&#x7136;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x4ED4;&#x7EC6;&#x8003;&#x8651;&#x4E00;&#x4E0B;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x8C03;&#x7528;&#x65B0;&#x51FD;&#x6570;&#x65F6;&#x6570;&#x636E;&#x7684;&#x72B6;&#x6001;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3.2-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 3.2 使用互斥量">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.4-chinese.html" class="navigation navigation-next " aria-label="Next page: 3.4 本章总结">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.3 保护共享数据的方式","level":"1.10.3","depth":2,"next":{"title":"3.4 本章总结","level":"1.10.4","depth":2,"path":"content/chapter3/3.4-chinese.md","ref":"content/chapter3/3.4-chinese.md","articles":[]},"previous":{"title":"3.2 使用互斥量","level":"1.10.2","depth":2,"path":"content/chapter3/3.2-chinese.md","ref":"content/chapter3/3.2-chinese.md","articles":[]},"dir":"ltr"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"plugins":["katex","ace"],"livereload":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"fontSettings":{"theme":"white","family":"msyh","size":2},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":10,"left":10,"top":10,"bottom":20}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"C++并发编程实战（第2版）","language":"zh-hans","gitbook":"*","description":"《C++ Concurrency In Action - SECOND EDITION》英文版的中文翻译版。","direction":"ltr"},"file":{"path":"content/chapter3/3.3-chinese.md","mtime":"2022-11-19T02:54:31.307Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-11-19T07:50:48.148Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

